package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.me.common.base.Const;
import com.me.common.base.Global;
import com.me.common.util.SharedPreUtil;
import com.me.jingdongshop.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YL on 2017/5/5.
 */

public class GuideActivity extends AppCompatActivity {
    private android.support.v4.view.ViewPager guidevp;
    private static final int[] IMAGE_IDS = {R.drawable.guide_1,R.drawable.guide_2,R.drawable.guide_3};
    private List<ImageView> imageContainers = new ArrayList<>();
    private android.widget.Button guidebtn;
    private android.widget.LinearLayout guidell;
    private ImageView guideimgred;
    private int mMaxLeft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        initView();
        initData();
        initEvent();

    }

    private void initView() {
        this.guideimgred = (ImageView) findViewById(R.id.guide_img_red);
        this.guidell = (LinearLayout) findViewById(R.id.guide_ll);
        this.guidevp = (ViewPager) findViewById(R.id.guide_vp);
        this.guidebtn = (Button) findViewById(R.id.guide_btn);
        guidebtn.setVisibility(View.GONE);
    }

    private void initData() {
        //找到相应id的图片储存起来
        for (int i = 0; i <IMAGE_IDS.length ; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(IMAGE_IDS[i]);
            imageContainers.add(imageView);
        }
        //要获取一些属性值的控件去获得树形观察者,监听是一个全局的布局状态或者view可见的时候会回调
        guideimgred.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                //拿到两个圆的距离
                mMaxLeft = guidell.getChildAt(1).getLeft() - guidell.getChildAt(0).getLeft();
                guideimgred.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }

    private void initEvent() {
        MyPagerAdapter adapter = new MyPagerAdapter();
        guidevp.setAdapter(adapter);

        guidevp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //position为当前角标，positionOffset为滑动的百分比[0,1),positionOffsetPixels位像素滑动值
                //这里要写父类的layoutparams，因为是相对父亲的参数
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) guideimgred.getLayoutParams();
                params.leftMargin = (int)(mMaxLeft*(positionOffset+position));
                guideimgred.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {
                //判断是最后一个页面就显示按钮，否则gone不显示
                if(position == imageContainers.size()-1){
                    guidebtn.setVisibility(View.VISIBLE);
                }else {
                    guidebtn.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //点击进入主界面
        guidebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //页面跳转
                startActivity(new Intent(GuideActivity.this,MainActivity.class));
                //保存是否第一次进入引导页
                SharedPreUtil.saveBoolean(Global.mContext, Const.ISFIRST_VISIT,false);
                //关闭引导页
                finish();
            }
        });
    }

    private class MyPagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return imageContainers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //将对应位置的对象保存到container
            ImageView imageView = imageContainers.get(position);
            container.addView(imageView);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //移除缓存的对象
            container.removeView((View) object);
        }
    }
}
